package main

import (
	"crypto/md5"
	"crypto/sha512"
	"encoding/hex"
	"fmt"
	"mxshop_srvs/user_srv/handler"

	"log"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
	"io"
	"mxshop_srvs/user_srv/model"
	"os"
	"time"
)

func getMd5(code string) string {
	MD5 := md5.New()
	_, _ = io.WriteString(MD5, code)
	return hex.EncodeToString(MD5.Sum(nil))
}

func main() {
	newLogger := logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags),
		logger.Config{
			SlowThreshold: time.Second * 2,
			LogLevel:      logger.Info,
			Colorful:      true,
		})
	dsn := "root:A123456@tcp(localhost:3306)/mxshop_user_srv?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: newLogger,
		// 单数表名
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("连接成功")
	// 建表
	db.AutoMigrate(&model.User{})

	// 插入用户
	options := &handler.Options{16, 100, 32, sha512.New}
	salt, encodedPwd := handler.Encode("admin123", options)
	newPwd := fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodedPwd)
	for i := 0; i < 10; i++ {
		user := model.User{
			NickName: fmt.Sprintf("name%d", i),
			Mobile: fmt.Sprintf("1878222222%d", i),
			Password: newPwd,
		}
		db.Save(&user)
	}
}